
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.merchant.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import cn.hutool.core.collection.CollUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lxk.merchant.domain.bo.MerMerchantBo;
import com.lxk.merchant.domain.vo.MerMerchantVo;
import com.lxk.merchant.domain.MerMerchant;
import com.lxk.merchant.mapper.MerMerchantMapper;
import com.lxk.merchant.service.IMerMerchantService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 商户Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-09-18
 */
@RequiredArgsConstructor
@Service
public class MerMerchantServiceImpl implements IMerMerchantService {

    private final MerMerchantMapper baseMapper;

   /**
    * 查询IdList商户Map
    */
    @Override
    public Map<Long, MerMerchantVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(MerMerchantVo::getId, v -> v));
    }

    /**
     * 查询商户Map
     */
    @Override
    public Map<Long, MerMerchantVo> queryMap(MerMerchantBo bo) {
        return  this.queryList(bo).stream().collect(Collectors.toMap(MerMerchantVo::getId, v -> v));
    }

    /**
     * 查询商户IdList
     */
    @Override
    public List<MerMerchantVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<MerMerchant> lqw = Wrappers.lambdaQuery();
        lqw.in(MerMerchant::getId, idList);
       return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询商户
     *
     * @param id 主键
     * @return 商户
     */
    @Override
    public MerMerchantVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询商户列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商户分页列表
     */
    @Override
    public TableDataInfo<MerMerchantVo> queryPageList(MerMerchantBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MerMerchant> lqw = buildQueryWrapper(bo);
        Page<MerMerchantVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商户列表
     *
     * @param bo 查询条件
     * @return 商户列表
     */
    @Override
    public List<MerMerchantVo> queryList(MerMerchantBo bo) {
        LambdaQueryWrapper<MerMerchant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MerMerchant> buildQueryWrapper(MerMerchantBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MerMerchant> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(MerMerchant::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            MerMerchant::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            MerMerchant::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.like(StringUtils.isNotBlank(bo.getMerchantName()), MerMerchant::getMerchantName, bo.getMerchantName());
        lqw.like(StringUtils.isNotBlank(bo.getContactPersonName()), MerMerchant::getContactPersonName, bo.getContactPersonName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhoneNumber()), MerMerchant::getContactPhoneNumber, bo.getContactPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantType()), MerMerchant::getMerchantType, bo.getMerchantType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmailAddress()), MerMerchant::getEmailAddress, bo.getEmailAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessScope()), MerMerchant::getBusinessScope, bo.getBusinessScope());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisteredAddress()), MerMerchant::getRegisteredAddress, bo.getRegisteredAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicenseNumber()), MerMerchant::getBusinessLicenseNumber, bo.getBusinessLicenseNumber());
        lqw.like(StringUtils.isNotBlank(bo.getBankName()), MerMerchant::getBankName, bo.getBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAccountNumber()), MerMerchant::getBankAccountNumber, bo.getBankAccountNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getLegalRepresentative()), MerMerchant::getLegalRepresentative, bo.getLegalRepresentative());
        lqw.eq(bo.getRegisteredCapital() != null, MerMerchant::getRegisteredCapital, bo.getRegisteredCapital());
        lqw.eq(bo.getEstablishmentDate() != null, MerMerchant::getEstablishmentDate, bo.getEstablishmentDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMailingAddress()), MerMerchant::getMailingAddress, bo.getMailingAddress());
        lqw.eq(bo.getUserId() != null, MerMerchant::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增商户
     *
     * @param bo 商户
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MerMerchantBo bo) {
        MerMerchant add = MapstructUtils.convert(bo, MerMerchant.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商户
     *
     * @param bo 商户
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MerMerchantBo bo) {
        MerMerchant update = MapstructUtils.convert(bo, MerMerchant.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MerMerchant entity){
    }

    /**
     * 校验并批量删除商户信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
